package com.zhan.ielts.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.Users;
import com.zhan.ielts.domain.ZhentiCompositionContent;
import com.zhan.ielts.domain.query.ZhentiCompositionContentQuery;
import com.zhan.ielts.domain.vo.ZhentiCompositionContentVo;
import com.zhan.ielts.service.UsersService;
import com.zhan.ielts.service.ZhentiCompositionContentService;

@Controller
@RequestMapping("/zhenti/composition/content")
public class ZhentiCompositionContentController extends
		BaseControllerImpl<ZhentiCompositionContent, ZhentiCompositionContentQuery> {

	private Logger log = LoggerFactory.getLogger(ZhentiCompositionContentController.class);

	@Autowired
	private ZhentiCompositionContentService zhentiCompositionContentService;

	@Autowired
	private UsersService usersService;

	@Override
	protected BaseService<ZhentiCompositionContent> getBaseService() {
		return zhentiCompositionContentService;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetch(String userId, Long updateTime) {
		log.debug("获取真题写作内容开始");
		Map map = new HashMap();

		// 检查userId 是否为空
		if (StringUtils.isEmpty(userId)) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		} else if (updateTime == null) {
			map.put("errCode", 1002);
			return new CustomResult(0, "updateTime为空！", map);
		}

		// 检查userId 是否有效
		Users query = new Users();
		query.setId(userId);
		if (usersService.queryCount(query) < 1) {
			map.put("errCode", 1003);
			return new CustomResult(0, "userId无效！", map);
		}

		try {
			ZhentiCompositionContentVo zhentiCompositionContent = new ZhentiCompositionContentVo();
			zhentiCompositionContent.setUpdateTime(updateTime);
			zhentiCompositionContent.setUserId(userId);
			map = zhentiCompositionContentService.fetchZhentiCompositionContent(zhentiCompositionContent);
			log.debug("获取真题写作内容结束");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("errCode", 1004);
			return new CustomResult(0, "系统错误！", map);
		}

		return new CustomResult(1, "", map);

	}

	@SuppressWarnings({ "unchecked", "deprecation", "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CustomResult update(String userId, String compositionContents, String device, String osVersion, String appVersion) {
		log.debug("更新真题作文内容开始");
		Map map = new HashMap();

		// 检查userId 是否为空
		if (StringUtils.isEmpty(userId)) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}

		try {
			// 检查userId 是否有效
			Users query = new Users();
			query.setId(userId);
			if (usersService.queryCount(query) < 1) {
				map.put("errCode", 1002);
				return new CustomResult(0, "userId无效！", map);
			}

			JSONArray jan = JSONArray.fromObject(compositionContents);
			List<ZhentiCompositionContent> zhentiCompositionContentList = JSONArray.toList(jan, ZhentiCompositionContent.class);

			// 更新真题作文内容
			zhentiCompositionContentService.updateZhentiCompositionContent(zhentiCompositionContentList, userId, device, osVersion, appVersion);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误！", map);
		}

		log.debug("更新真题作文内容结束");
		return new CustomResult(1, "", map);
	}
	
}
